import * as stepCache from '../../src/tool/cache'
import path from 'path'
import {expect} from 'chai'
import process from 'process'
import fs from 'fs'

describe('tool cache test case', () => {
    describe('cache dir test case', () => {
        afterEach(function () {
            const toBeRemovedPath = path.join(__dirname, 'caches', 'target')
            fs.rmSync(toBeRemovedPath, {recursive: true, force: true})
        })
        it('after cache successfully, should create completed file', async () => {
            process.env.FLOW_RUNNER_TOOL_CACHE_DIR = path.join(
                __dirname,
                'caches',
                'target'
            )
            const sourceDir = path.join(__dirname, 'caches', 'sources')
            const cachedDir = await stepCache.cacheDir(
                sourceDir,
                'tool1',
                '1.0.1',
                'x64'
            )
            expect(cachedDir).to.equal(
                path.join(
                    __dirname,
                    'caches',
                    'target',
                    'tool1',
                    '1.0.1',
                    'x64'
                )
            )

            const targetDirPath = path.join(
                __dirname,
                'caches',
                'target',
                'tool1',
                '1.0.1',
                'x64'
            )
            expect(fs.existsSync(path.join(targetDirPath, 'dir1'))).to.be.ok
            expect(fs.existsSync(path.join(targetDirPath, 'dir1', 'file2'))).to
                .be.ok
            expect(fs.existsSync(path.join(targetDirPath, 'file1'))).to.be.ok
            expect(fs.existsSync(path.join(targetDirPath, 'cache.completed')))
                .to.be.ok
        })
    })

    describe('cache file test case', () => {
        afterEach(function () {
            const toBeRemovedPath = path.join(__dirname, 'caches', 'target')
            fs.rmSync(toBeRemovedPath, {recursive: true, force: true})
        })
        it('after cache successfully, should create completed file', async () => {
            process.env.FLOW_RUNNER_TOOL_CACHE_DIR = path.join(
                __dirname,
                'caches',
                'target'
            )
            const sourceFile = path.join(
                __dirname,
                'caches',
                'sources',
                'file1'
            )
            const cachedFile = await stepCache.cacheFile(
                sourceFile,
                'file1',
                'tool2',
                '1.0.2',
                'arm64'
            )
            expect(cachedFile).to.equal(
                path.join(
                    __dirname,
                    'caches',
                    'target',
                    'tool2',
                    '1.0.2',
                    'arm64'
                )
            )

            const targetDirPath = path.join(
                __dirname,
                'caches',
                'target',
                'tool2',
                '1.0.2',
                'arm64'
            )
            expect(fs.existsSync(path.join(targetDirPath, 'file1'))).to.be.ok
            expect(fs.existsSync(path.join(targetDirPath, 'cache.completed')))
                .to.be.ok
        })
    })
})
